<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { authApi } from '../../services/api'

const router = useRouter()
const isLoggedIn = ref(false)

// 检查用户登录状态
const checkLoginStatus = async () => {
  try {
    await authApi.getProfile()
    isLoggedIn.value = true
  } catch (error) {
    // 当用户未登录时，会收到401错误，这是预期行为
    // 我们只将登录状态设置为false，而不将错误输出到控制台
    isLoggedIn.value = false
  }
}

// 用户登出
const handleLogout = () => {
  authApi.logout()
  isLoggedIn.value = false
  router.push('/login')
}

onMounted(() => {
  checkLoginStatus()
})

// 监听路由变化，更新登录状态
router.afterEach(() => {
  checkLoginStatus()
})
</script>

<template>
  <nav class="navbar neo-brutalist">
    <div class="nav-container">
      <div class="nav-brand" @click="router.push('/')">
        虚拟Emoji角色系统
      </div>
      
      <ul class="nav-menu">
        <li class="nav-item">
          <a @click="router.push('/')" class="nav-link">首页</a>
        </li>
        
        <template v-if="isLoggedIn">
          <li class="nav-item">
            <a @click="router.push('/character-create')" class="nav-link">创建角色</a>
          </li>
          <li class="nav-item">
            <a @click="router.push('/chat')" class="nav-link">聊天</a>
          </li>
          <li class="nav-item">
            <a @click="router.push('/diary')" class="nav-link">日记</a>
          </li>
          <li class="nav-item">
            <a @click="router.push('/world')" class="nav-link">虚拟世界</a>
          </li>
          <li class="nav-item">
            <button class="nav-btn neo-brutalist btn btn--danger" @click="handleLogout">登出</button>
          </li>
        </template>
        
        <template v-else>
          <li class="nav-item">
            <button class="nav-btn neo-brutalist btn btn--primary" @click="router.push('/register')">注册</button>
          </li>
          <li class="nav-item">
            <button class="nav-btn neo-brutalist btn btn--secondary" @click="router.push('/login')">登录</button>
          </li>
        </template>
      </ul>
    </div>
  </nav>
</template>

<style scoped>
.navbar {
  background-color: white;
  padding: 0;
  margin-bottom: 2rem;
  border: 4px solid #000;
  box-shadow: 8px 8px 0 #000;
  transition: all 0.2s ease;
}

.navbar:hover {
  box-shadow: 6px 6px 0 #000;
  transform: translate(2px, 2px);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 70px;
}

.nav-brand {
  font-size: 1.5rem;
  font-weight: bold;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 2px;
  color: #000;
  cursor: pointer;
}

.nav-menu {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 1rem;
  align-items: center;
}

.nav-item {
  display: flex;
  align-items: center;
}

.nav-link {
  color: #000;
  text-decoration: none;
  font-size: 1.1rem;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 1px;
  cursor: pointer;
  padding: 0.5rem;
  position: relative;
  text-transform: uppercase;
  border: 2px solid transparent;
  transition: all 0.2s ease;
}

.nav-link:hover {
  color: #EF476F;
  border: 2px solid #EF476F;
}



button.nav-btn {
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  transition: all 0.2s ease;
  font-weight: bold;
  padding: 0.5rem 1rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1rem;
  margin: 0;
}

button.nav-btn:hover {
  box-shadow: 2px 2px 0 #000;
  transform: translate(2px, 2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
    height: auto;
    padding: 1rem 0;
  }
  
  .nav-menu {
    margin-top: 1rem;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .nav-item {
    margin: 0.25rem;
  }
}
</style>